﻿
var treeobjedit = {
    formDefine: {
        labelWidth: 90, inputWidth: 250, space: 10, labelAlign: 'right',
        fields: [
                    { display: lang.node_name + "：", name: "ObjectName", type: "text", newline: true, width: 280, serviceDataName: 'ObjectName' },
                    { display: lang.parent_node_locaion + "：", name: "PObjectID", type: "selecttree", newline: true, inputWidth: 250, dataoptions: "panelHeight:200" },
                    { display: lang.sequence + "：", name: "SN", type: "text", newline: true, serviceDataName: 'SN' },
                    { display: lang.searchSql + "：", name: "SH_SQL", type: "text", newline: true, serviceDataName: 'SH_SQL' },
        //                    { display: lang.address + "：", name: "SH_SQL", type: "textarea", newline: true, width: 280, serviceDataName: 'SH_SQL' },
                    {display: lang.Remarks + "：", name: "ObjectTypeDesc", type: "text", serviceDataName: 'ObjectTypeDesc', newline: true },
                    { display: lang.save, name: "search", type: "button", newline: true, nlstyle: "text-align:center", width: 280, click: function () { treeobjedit.form.submit(); } },
                    { display: lang.close, name: "close", type: "button", width: 280, click: function () {
                        parent.$.closePage();
                    }
                    }
                ]
                ,
        onLoaded: function (data, form) {
            var _currentval = '';
            if (data) {
                _currentval = data.PObjectID;
            }
            loadOrgData(_currentval);
        }
                ,
        submitUrl: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        submitUrlType: "EditTreeOjbect",
        submitCheck: function (params) {

            if (!params["ObjectName"] || params["ObjectName"] == "") {
                return lang.node_name_can_not_be_empty;
            }
            if (!params["PObjectID"] || params["PObjectID"] == "") {
                return lang.The_parent_node_locaion_cannot_be_empty;
            }
            if (isNaN(params["SN"])) {
                return lang.sequence_must_be_digital;
            }

            return "";
        },
        submitParams: function (params) {
            if ($.getPara('objid') && $.getPara('objid') != '') {
                params.action = 1;
                params.ObjectID = $.getPara('objid');
                params.TypeID = $.getPara('typeid');
            } else {
                params.action = 2;
                params.TypeID = $.getPara('typeid');
            }
            params.IsValid = 1;

            return params;
        },
        submitSuccess: function () {
            if ($.getPara('objid') && $.getPara('objid') != '') {
                $.alert(lang.node_modified_success, null, function () { parent.treeobjlist.search(); parent.$.closePage(); });
            } else {
                $.alert(lang.node_add_success, null, function () { parent.treeobjlist.search(); parent.$.closePage(); });
            }
        }
    },
    init: function () {
        if ($.getPara('objid') != '' && $.getPara('typeid') != '') {
            this.formDefine.url = App_Config.TadongUrl + "/AjaxMappingHandler.aspx";
            this.formDefine.urlType = "GetTreeInfoByID";
            this.formDefine.params = function () { return $.toJSON({ ojbID: $.getPara('objid'), desType: $.getPara('typeid') }); };
        }
        jsPageRender(this);
    }
};


//#region 加载组织数据
function loadOrgData(_currentval) {

    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryAllTree",
        model: function () { return $.getPara('typeid'); },
        success: function (data) {
            if (data && data.length > 0) {

                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PObjectID == '0') {

                        var obj = {};
                        obj.id = data[i].ObjectID;

                        obj.text = data[i].ObjectName;
                        getsubOrg(data, data[i].ObjectID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidPObjectID').combotree({ multiple: false });
                $('#formidPObjectID').combotree('loadData', _dataarr);
//                if (_currentval != '') {
                    $('#formidPObjectID').combotree('setValue', _currentval);
//                }

            } else {
                $('#formidPObjectID').combotree({ multiple: false });
                $('#formidPObjectID').combotree('loadData', [{ id: 0, text: lang.root}]);
            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PObjectID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ObjectID;
                    obj.text = _d[i].ObjectName;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}

//#endregion